package grape.crm.rest.company.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.common.rest.mvc.BaseTreeController;
import grape.crm.service.company.query.CompanyListQuery;
import grape.crm.service.customer.api.ICustomerService;
import grape.crm.service.customer.po.Customer;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.crm.rest.company.form.CompanyCreateForm;
import grape.crm.rest.company.form.CompanyUpdateForm;
import grape.crm.rest.company.form.CompanyListPageForm;
import grape.crm.rest.company.vo.CompanyVo;
import grape.crm.rest.company.mapper.CompanyWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.crm.service.company.po.Company;
import grape.crm.service.company.api.ICompanyService;
import java.util.List;
import grape.common.rest.vo.TreeNodeVo;
/**
 * <p>
 * 公司 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-14
 */
@RestController
@RequestMapping("/crm/company")
@Api(tags = "公司相关接口")
public class CompanyController extends BaseTreeController<CompanyVo, Company> {

    @Autowired
    private CompanyWebMapper currentWebMapper;
    @Autowired
    private ICompanyService currentService;
    @Autowired
    private ICustomerService iCustomerService;



     @ApiOperation("添加公司")
     @PreAuthorize("hasAuthority('company:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public CompanyVo create(@RequestBody @Valid CompanyCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }
         // 客户编码检查
         if (!isStrEmpty(cf.getLegalCustomerCode())) {
             Customer byCode = iCustomerService.getByCode(cf.getLegalCustomerCode());
             if (byCode == null) {
                 throw new RBaseException("客户编码不存在");
             }
             cf.setLegalCustomerId(byCode.getId());
         }

         Company po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询公司")
     @PreAuthorize("hasAuthority('company:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public CompanyVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除公司")
     @PreAuthorize("hasAuthority('company:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新公司")
     @PreAuthorize("hasAuthority('company:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public CompanyVo update(@PathVariable String id,@RequestBody @Valid CompanyUpdateForm uf) {
         // 客户编码检查
         if (!isStrEmpty(uf.getLegalCustomerCode())) {
             Customer byCode = iCustomerService.getByCode(uf.getLegalCustomerCode());
             if (byCode == null) {
                 throw new RBaseException("客户编码不存在");
             }
             uf.setLegalCustomerId(byCode.getId());
         }
         Company po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("分页查询公司")
    @PreAuthorize("hasAuthority('company:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<CompanyVo> listPage(CompanyListPageForm listPageForm) {
         Company po = currentWebMapper.formToPo(listPageForm);
        CompanyListQuery companyListQuery = new CompanyListQuery();
        companyListQuery.setPoQuery(po);
        return super.listPage(companyListQuery,listPageForm);
     }

    @ApiOperation("公司树")
    @PreAuthorize("hasAuthority('company:single:tree')")
    @GetMapping("/tree")
    @ResponseStatus(HttpStatus.OK)
    public List<TreeNodeVo<CompanyVo>> tree( String parentId) {
        return super.listToTreeNodeVo(super.getByParentId(parentId));
    }
}
